package com.geniusk.system.service.impl;

import com.geniusk.system.model.mapper.ModelActionMapper;
import com.geniusk.system.model.mapper.ModelEventMapper;
import com.geniusk.system.model.mapper.ModelMapper;
import com.geniusk.system.model.mapper.ModelPropertyMapper;
import com.geniusk.system.model.pojo.Model;
import com.geniusk.system.model.pojo.ModelAction;
import com.geniusk.system.model.pojo.ModelEvent;
import com.geniusk.system.model.pojo.ModelProperty;
import com.geniusk.system.model.vo.*;
import com.geniusk.system.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ModelServiceImpl implements ModelService {

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private ModelPropertyMapper modelPropertyMapper;

    @Autowired
    private ModelEventMapper modelEventMapper;

    @Autowired
    private ModelActionMapper modelActionMapper;

    @Override
    public Model selectModelById(Long modelId) {
        return modelMapper.selectModelById(modelId);
    }

    @Override
    public List<Model> selectModelByPage(Model model) {
        return modelMapper.selectModelByPage(model);
    }

    @Override
    public List<Model> selectAllModel() {
        return modelMapper.selectAllModel();
    }

    /**
     * 获取模型结构
     * @param modelId 模型ID
     * @return 模型结构
     */
    public ModelVO selectModelVOByModelId(Long modelId){
        ModelVO modelVO = new ModelVO();
        modelVO.setProductId(modelId.toString());
        modelVO.setVersion("1.0.0");
        List<ModelProperty> modelProperties = modelPropertyMapper.selectModelPropertyByModelId(modelId);
        List<PropertyVO> properties = getProperties(modelProperties);
        modelVO.setProperties(properties);
        List<ModelEvent> modelEvents = modelEventMapper.selectModelEventByModelId(modelId);
        List<EventVO> events = new ArrayList<>();
        for(ModelEvent modelEvent : modelEvents){
            EventVO eventVO = new EventVO();
            eventVO.setId(modelEvent.getEvent());
            eventVO.setName(modelEvent.getEventName());
            eventVO.setDesc(modelEvent.getRemark());
            eventVO.setRequired(true);
            List<ModelProperty> eventProperties = modelEvent.getProperties();
            List<PropertyVO> params = getProperties(eventProperties);
            eventVO.setParams(params);
            events.add(eventVO);
        }
        modelVO.setEvents(events);
        List<ModelAction> modelActions = modelActionMapper.selectModelActionByModelId(modelId);
        List<ActionVO> actions = new ArrayList<>();
        for(ModelAction modelAction : modelActions){
            ActionVO actionVO = new ActionVO();
            actionVO.setId(modelAction.getAction());
            actionVO.setName(modelAction.getActionName());
            actionVO.setDesc(modelAction.getRemark());
            List<ModelProperty> actionProperties = modelAction.getProperties();
            List<PropertyVO> input = getProperties(actionProperties);
            actionVO.setInput(input);
            actions.add(actionVO);
        }
        modelVO.setActions(actions);
        return modelVO;
    }

    /**
     * 获取属性
     * @param properties 模型属性
     * @return 属性
     */
    private List<PropertyVO> getProperties(List<ModelProperty> properties) {
        List<PropertyVO> params = new ArrayList<>();
        for(ModelProperty modelProperty : properties){
            PropertyVO propertyVO = new PropertyVO();
            propertyVO.setId(modelProperty.getField());
            propertyVO.setName(modelProperty.getFieldName());
            propertyVO.setDesc(modelProperty.getRemark());
            propertyVO.setRequired(modelProperty.getWritable().equals("1"));
            propertyVO.setUnit(modelProperty.getFieldUnit());
            PropertyDefineVO propertyDefineVO = new PropertyDefineVO();
            propertyDefineVO.setType(modelProperty.getFieldType());
            propertyVO.setDefine(propertyDefineVO);
            params.add(propertyVO);
        }
        return params;
    }

    @Override
    public int insertModel(Model model) {
        return modelMapper.insertModel(model);
    }

    @Override
    public int updateModel(Model model) {
        return modelMapper.updateModel(model);
    }

    @Override
    public int deleteModelByIds(Long[] ids) {
        return modelMapper.deleteModelByIds(ids);
    }
}
